//
//  AppTools.m
//  UI14_json解析
//
//  Created by dllo on 15/12/9.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "AppTools.h"
#import "AFNetworking.h"

@implementation AppTools

// 封装AFN方法
+ (void)getDataWithAFNURL:(NSString *)urlStr block:(AppToolsBlock)block errorBlock:(AppToolsErrorBlock)errorBlock{
    
    
    
    NSString *url_string = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    //请求管理者 所有的网络请求,均有manager发起
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        // 回调
        block(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 失败时回调
        errorBlock(error);
        NSLog(@"失败==== %@",error);
        
    }];
    
    
    
}


+ (void)getDataFromJsonWithFileName:(NSString *)fileName Block:(AppToolsBlock)block error:(AppToolsErrorBlock)errorBlock{
    
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
//    NSData *data = [NSData dataWithContentsOfFile:filePath];

    NSString *urlMovie = fileName;
    NSString *urlEncode = [urlMovie stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlEncode];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configure];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        if (error == nil) {
            block(result);
//            NSLog(@"%@", result);
        }else{
            errorBlock(error);
            
        }
        
    }];
    [task resume];
    
    

    
    
//    NSError *error = nil;
//    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
//    if (error == nil) {
//        block(result);
//    }else{
//    errorBlock(error);
//
//    }
}


+ (BOOL)checkImageIsExistOnLocal:(NSString *)imageUrl onFolders:(NSArray <NSString *> *)folders{
    NSString *picPath = [imageUrl lastPathComponent];
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *finalPath = [document copy];
    for (NSString *folder in folders) {
        finalPath = [finalPath stringByAppendingPathComponent:folder];
    }
    finalPath = [finalPath stringByAppendingPathComponent:picPath];
    return [[NSFileManager defaultManager] fileExistsAtPath:finalPath];
}

+ (NSString *)createImageLocalPathUseUrl:(NSString *)imageUrl withFolders:(NSArray <NSString *> *)folders{
    NSString *picPath = [imageUrl lastPathComponent];
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *finalPath = [document copy];
    for (NSString *folder in folders) {
        finalPath = [finalPath stringByAppendingPathComponent:folder];
    }
    
    [[NSFileManager defaultManager] createDirectoryAtPath:finalPath withIntermediateDirectories:YES attributes:nil error:nil];
    
    finalPath = [finalPath stringByAppendingPathComponent:picPath];
    return finalPath;
}

+ (BOOL)saveImageOnLocal:(UIImage *)image localPath:(NSString *)path{
    NSData *data = UIImageJPEGRepresentation(image, 1);
    if (data == nil) {
        NSLog(@"无效图片文件, 保存失败");
        return NO;
    } else {
        [data writeToFile:path atomically:YES];
        return YES;
    }
}

+ (NSString *)createFilePathFromDocumentWithFolders:(NSArray<NSString *> *)folders fileName:(NSString *)fileName{
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *finalPath = [NSString stringWithFormat:@"%@", document];
    for (NSString *folder in folders) {
        finalPath = [finalPath stringByAppendingPathComponent:folder];
    }
    
    [[NSFileManager defaultManager] createDirectoryAtPath:finalPath withIntermediateDirectories:YES attributes:nil error:nil];
    
    finalPath = [finalPath stringByAppendingPathComponent:fileName];
    return finalPath;
}





@end
